package com.example.statistics.ui.port

import android.os.Bundle
import com.example.statistics.PriceItemBean
import com.example.statistics.R
import com.example.statistics.bean.Tax
import com.example.statistics.common.BaseActivity
import com.example.statistics.constant.PreferenceConstants
import com.example.statistics.ex.*
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.activity_estimate.*

/**
 * 成本估算
 */
class EstimateActivity : BaseActivity() {


    private var taxBean: Tax? = null

    private val productAmount
        get() = tv_amount.textStringToNumber.mulEx(tv_rate.textStringToNumber, 4)


    override val title: String
        get() = "成本估算"

    //货款 = 美元 * 汇率


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_estimate)

        //获取上次关闭时的汇率
        val exchangeRate = preferenceFind(String::class.java, PreferenceConstants.EXCHANGE_RATE)
        tv_rate.contentText = exchangeRate

        //获取不同商品的关税列表
        val taxList = assets<List<Tax>>("tax.json")
        taxBean = taxList[0]

        tv_type.contentText = "最惠国"
        tv_type.onContentClicked {
            singleDialog("最惠国", "美国") {
                tv_type.contentText = it
                productPrice()
            }
        }


        tv_tax.contentText = "${taxBean!!.defaultTariff + 1} * ${taxBean!!.valueAddTax + 1}"
        tv_tax.onContentClicked {
            singleDialog(taxList) {
                taxBean = it
                productPrice()
            }
        }

        tv_amount.afterTextChangedListener { _, _ ->
            productPrice()
        }
        tv_rate.afterTextChangedListener { _, _ ->
            productPrice()
        }

        tv_price.contentText = """
            商品单价 = 采购单价 * 海关汇率 
            含关税单价 = 商品单价 * 海关关税
            含增值税单价 = 含关税单价 * 海关增值税 
        """.trimIndent()
    }


    fun productPrice() {
        taxBean?.let {
            val valueAddTax = it.valueAddTax.addEx(1, 4)
            var defaultTariff = it.defaultTariff
            if (tv_type.contentText == "美国") {
                defaultTariff = it.americaTariff
            }
            if (it.isWeight) {
                // （单价+每公斤的关税）* 增值税
                tv_tax.contentText = "($productAmount + ${defaultTariff}) * ${it.valueAddTax + 1}"
                if (productAmount.toFloat() != 0f)
                    tv_total.contentText = productAmount
                        .addEx(it.defaultTariff, 4)
                        .mulEx(valueAddTax, 4).toString()
            } else {
                // 单价 * ( 1 + 关税) * (1 + 增值税)
                tv_tax.contentText = "${defaultTariff + 1} * ${it.valueAddTax + 1}"
                if (productAmount.toFloat() != 0f)
                    tv_total.contentText = productAmount
                        .mulEx(defaultTariff + 1, 4).
                        mulEx(valueAddTax, 4).toString()
            }
        }
    }

    override fun onStop() {
        super.onStop()
        preferenceSave(tv_rate.contentText, PreferenceConstants.EXCHANGE_RATE)
    }
}